home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 8206 / 8206.xpi / content / appspecific.js next >
Text File  |  2010-02-02  |  11KB  |  408 lines

  1. function WisestampGetAppObject()
  2. {
  3.     if (this.application) 
  4.         return this.application;
  5.  
  6.     if (WiseStampUtils.getAppType() == "Thunderbird")
  7.         this.application = new WisestampThunderbird();
  8.     else
  9.         this.application = new WisestampFirefox();
  10.     
  11.     return this.application;
  12. }
  13.     
  14.     
  15. function WisestampFirefox()
  16. {
  17.  
  18. }
  19.  
  20. WisestampFirefox.prototype =
  21. {
  22.     onLoad: function ()
  23.     {
  24.         setTimeout(function (self){ 
  25.             document.addEventListener("ShowWisestampMenu", self.handleEvent, false, true); }, 1000, this);
  26.         var contextmenu = document.getElementById("contentAreaContextMenu");
  27.         if (contextmenu) 
  28.             contextmenu.addEventListener("popupshowing", this.handleEvent, false);
  29.     },
  30.  
  31.     openFirstRun: function ()
  32.     {
  33.         var firstrunURL = "http://www.wisestamp.com/first-steps";
  34.         this.openSite(firstrunURL);
  35.     },
  36.  
  37.     openUpdate: function (currentVersion)
  38.     {
  39.         var updateURL = "http://www.wisestamp.com/version-history-upgraded#Version_" + currentVersion;
  40.         this.openSite(updateURL);
  41.     },
  42.  
  43.     onUseSignature: function (event, aType, aPopup)
  44.     {
  45.         WiseStampUtils.log("[appspecific.js::Firefox::onUseSignature] >>>>> ");
  46.         
  47.         if (typeof gContextMenu != 'undefined' && gContextMenu) 
  48.             WisestampSignatureFactory.insertSignature(aType, gContextMenu.target.ownerDocument);
  49.         else if (this._contextEvent) 
  50.             this.updateSiteFromWebmail(aType);
  51.         else 
  52.             this.updateSiteFromChrome(aType);
  53.     },
  54.  
  55.     handleEvent: function (event)
  56.     {
  57.         if (event.type == "popupshowing")
  58.         {
  59.             WisestampOverlay.app.showContextMenu(event);
  60.         } else if (event.type == "ShowWisestampMenu")
  61.         {
  62.             WisestampOverlay.app.showWisestampMenuForContent(event);
  63.         }
  64.     },
  65.  
  66.     showContextMenu: function (event)
  67.     {
  68.         var item = document.getElementById("context-wisestamp");
  69.         item.hidden = !(WisestampOverlay.enabled && gContextMenu.isDesignMode);
  70.     },
  71.  
  72.     updateSiteFromWebmail: function updateSiteFromWebmail_name(aType)
  73.     {
  74.         WiseStampUtils.log("[appspecific.js::updateSiteFromWebmail] >>>>> ");
  75.         
  76.         var event = this._contextEvent;
  77.         if (event)
  78.         {
  79.             event.target.setAttribute("sigtype", aType);
  80.             var doc = event.target.ownerDocument;
  81.             var evt = doc.createEvent("Events");
  82.             evt.initEvent("UpdateWisestampSignature", true, false);
  83.  
  84.             var canceled = !event.target.dispatchEvent(evt);
  85.         }
  86.     },
  87.  
  88.     updateSiteFromChrome: function updateSiteFromChrome_name(aType)
  89.     {
  90.         WiseStampUtils.log("[appspecific.js::updateSiteFromChrome] >>>>> ");
  91.         
  92.         if (typeof wisestamppreview != "undefined")
  93.         {
  94.             load();
  95.             
  96.         } else
  97.         {
  98.             var PREFIX = "<!--WISESTAMP_SIG_START-->";
  99.             var SUFFIX = "<!--WISESTAMP_SIG_END-->";
  100.             
  101.             function scanFrames(win)
  102.             {
  103.                 for (var i = 0; i < win.frames.length; i++)
  104.                 {
  105.                     var f = win.frames[i];
  106.                     if (f.document.designMode == "on")
  107.                     {
  108.                         var pattern = new RegExp(PREFIX.replace("!", "\!") + "(.|\n)*" + SUFFIX.replace("!", "\!"));
  109.                         var sig = PREFIX + WisestampSignatureFactory.createSignature(null, aType) + SUFFIX;
  110.                         var body = f.document.body;
  111.                         if (body.innerHTML.match(pattern))
  112.                         {
  113.                             //Components.utils.reportError("sig in body");
  114.                             body.innerHTML = body.innerHTML.replace(pattern, sig.replace('$','$$$$'));
  115.                         } else
  116.                         {
  117.                             //Components.utils.reportError("inserted at end");
  118.                             body.innerHTML = body.innerHTML + "<br><br>" + sig;
  119.                         }
  120.                     } else
  121.                     {
  122.                         scanFrames(f);
  123.                     }
  124.                 }
  125.             }
  126.             scanFrames(content);
  127.         }
  128.     },
  129.  
  130.     showWisestampMenuForContent: function (event)
  131.     {
  132.         this._contextEvent = event;
  133.         var popup = document.getElementById("wisestampStatusPopup");
  134.         var x = event.screenX;
  135.         var y = event.screenY;
  136.         if (popup.openPopupAtScreen) //FF3
  137.             popup.openPopupAtScreen(x, y, false);
  138.         else
  139.             popup.showPopup(document.documentElement, x, y, "popup", "bottomleft", "topleft");
  140.     },
  141.     
  142.     onPopupHidden: function (event)
  143.     {
  144.         this._contextEvent = null;
  145.     },
  146.     openHelp: function ()
  147.     {
  148.         this.openSite('http://www.wisestamp.com/usage-faq');
  149.     },
  150.     
  151.     saveSignature: function (aData)
  152.     {
  153.  
  154.     },
  155.     
  156.     openSite: function (aURL)
  157.     {
  158.         var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"].getService(Components.interfaces.nsIWindowMediator);
  159.         var win = wm.getMostRecentWindow("navigator:browser");
  160.         if (win)
  161.         {
  162.             win.getBrowser().selectedTab = win.getBrowser().addTab(aURL);
  163.             win.focus();
  164.         } else
  165.         {
  166.             win = window.open(aURL);
  167.             win.focus();
  168.         }
  169.     },
  170.     enable: function ()
  171.     {
  172.  
  173.     },
  174.     disable: function ()
  175.     {
  176.  
  177.     },
  178.     getFooter: function (strings)
  179.     {
  180.         var footer = strings.GetStringFromName("wisestamp-footer-firefox");
  181.         return footer;
  182.     },
  183.     updateUI: function()
  184.     {
  185.     }
  186. }
  187.  
  188. /////////////////////////////////////////////////////////////////////
  189. /////////////////////////////////////////////////////////////////////
  190. /////////////////////////////////////////////////////////////////////
  191. function WisestampThunderbird()
  192. {
  193.  
  194. }
  195.  
  196. WisestampThunderbird.prototype =
  197. {
  198.     get prefs()
  199.     {
  200.         if (typeof WisestampOverlay != "undefined") 
  201.             return WisestampOverlay.prefs;
  202.         else 
  203.             return WiseStampOptions.PREFS;
  204.     },
  205.  
  206.     onLoad: function ()
  207.     {
  208.         this._saveSignatureToFile(this.prefs.getCharPref("type"));
  209.  
  210.         var prefs = Components.classes["@mozilla.org/preferences-service;1"].
  211.         getService(Components.interfaces.nsIPrefService);
  212.         prefs = prefs.getBranch("mail.");
  213.         prefs.setIntPref("default_html_action", 3);
  214.     },
  215.  
  216.     openFirstRun: function ()
  217.     {
  218.         var firstrunURL = "http://www.wisestamp.com/first-steps-thunderbird";
  219.         this.openSite(firstrunURL);
  220.     },
  221.  
  222.     openUpdate: function (currentVersion)
  223.     {
  224.         var updateURL = "http://www.wisestamp.com/version-history-upgraded#Version_" + currentVersion;
  225.         this.openSite(updateURL);
  226.     },
  227.  
  228.     onUseSignature: function (event, aType, aPopup)
  229.     {
  230.         WiseStampUtils.log("[appspecific.js::Thunderbird::onUseSignature] >>>>> ");
  231.         
  232.     },
  233.  
  234.     openSite: function (aURL)
  235.     {
  236.         var ioservice = Components.classes["@mozilla.org/network/io-service;1"].getService(Components.interfaces.nsIIOService);
  237.         var uriToOpen = ioservice.newURI(aURL, null, null);
  238.         var extps = Components.classes["@mozilla.org/uriloader/external-protocol-service;1"].getService(Components.interfaces.nsIExternalProtocolService);
  239.         extps.loadURI(uriToOpen, null);
  240.     },
  241.  
  242.     openHelp: function ()
  243.     {
  244.         this.openSite('http://www.wisestamp.com/usage-faq');
  245.     },
  246.  
  247.     _saveSignatureToFile: function (aType, aData)
  248.     {
  249.         WiseStampUtils.log("[appspecific.js::Thunderbird::_saveSignatureToFile] >>>>> ");
  250.         return;
  251.         
  252.         var file = Components.classes["@mozilla.org/file/directory_service;1"].
  253.         getService(Components.interfaces.nsIProperties).
  254.         get("ProfD", Components.interfaces.nsIFile).
  255.         QueryInterface(Components.interfaces.nsILocalFile);
  256.         file.append("wisestampsignature.html");
  257.  
  258.         // file is nsIFile, data is a string
  259.         var foStream = Components.classes["@mozilla.org/network/file-output-stream;1"].
  260.         createInstance(Components.interfaces.nsIFileOutputStream);
  261.  
  262.         // use 0x02 | 0x10 to open file for appending.
  263.         foStream.init(file, 0x02 | 0x08 | 0x20, 0666, 0);
  264.         // write, create, truncate
  265.         // In a c file operation, we have no need to set file mode with or operation,
  266.         // directly using "r" or "w" usually.
  267.         // if you are sure there will never ever be any non-ascii text in data you can 
  268.         // also call foStream.writeData directly
  269.         var converter = Components.classes["@mozilla.org/intl/converter-output-stream;1"].
  270.         createInstance(Components.interfaces.nsIConverterOutputStream);
  271.         converter.init(foStream, "UTF-8", 0, 0);
  272.         converter.writeString(WisestampSignatureFactory.getFullSignatureHTML(aType, aData));
  273.         converter.close(); // this closes foStream
  274.         var mailprefs = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService);
  275.         mailprefs = mailprefs.getBranch("mail.identity.");
  276.         for (var index = 1; true; index++)
  277.         {
  278.             //mail.identity.id1.attach_signature
  279.             //var prefName="id"+index+".attach_signature";
  280.             try
  281.             {
  282.                 mailprefs.getCharPref("id" + index + ".useremail");
  283.                 mailprefs.setBoolPref("id" + index + ".attach_signature", this.prefs.getBoolPref("autoinsert") && this.prefs.getBoolPref("enabled"));
  284.                 mailprefs.setCharPref("id" + index + ".sig_file-rel", "[ProfD]wisestampsignature.html");
  285.                 mailprefs.setCharPref("id" + index + ".sig_file", file.path);
  286.             } catch(e)
  287.             {
  288.                 break;
  289.             }
  290.         }
  291.     },
  292.     saveSignature: function (aType)
  293.     {
  294.         this._saveSignatureToFile(aType);
  295.     },
  296.     disable: function ()
  297.     {
  298.         var mailprefs = Components.classes["@mozilla.org/preferences-service;1"].
  299.         getService(Components.interfaces.nsIPrefService);
  300.         mailprefs = mailprefs.getBranch("mail.identity.");
  301.         for (var index = 1; true; index++)
  302.         {
  303.             //mail.identity.id1.attach_signature
  304.             var prefName = "id" + index + ".attach_signature";
  305.             try
  306.             {
  307.                 var val = mailprefs.getBoolPref(prefName);
  308.                 var sigfile = "";
  309.                 try
  310.                 {
  311.                     sigfile = mailprefs.getCharPref("id" + index + ".sig_file-rel");
  312.                 } catch(e)
  313.                 {
  314.                     sigfile = mailprefs.getCharPref("id" + index + ".sig_file");
  315.                 }
  316.                 if (sigfile.indexOf("wisestampsignature.html") > 0)
  317.                 {
  318.                     mailprefs.setBoolPref(prefName, false);
  319.                 } else
  320.                 {
  321.  
  322.                 }
  323.             } catch(e)
  324.             {
  325.                 break;
  326.             }
  327.         }
  328.     },
  329.     enable: function ()
  330.     {
  331.         var mailprefs = Components.classes["@mozilla.org/preferences-service;1"].
  332.         getService(Components.interfaces.nsIPrefService);
  333.         mailprefs = mailprefs.getBranch("mail.identity.");
  334.         for (var index = 1; true; index++)
  335.         {
  336.             //mail.identity.id1.attach_signature
  337.             var prefName = "id" + index + ".attach_signature";
  338.             try
  339.             {
  340.                 mailprefs.getBoolPref(prefName);
  341.                 mailprefs.setBoolPref(prefName, this.prefs.getBoolPref("autoinsert") && this.prefs.getBoolPref("enabled"));
  342.             } catch(e)
  343.             {
  344.                 break;
  345.             }
  346.         }
  347.     },
  348.     
  349.     updateSiteFromChrome: function updateSiteFromChrome_name(aType)
  350.     {
  351.         WiseStampUtils.log("[appspecific.js::Thunderbird::updateSiteFromChrome] >>>>> ");
  352.         if (typeof wisestamppreview != "undefined")
  353.         {
  354.             load();
  355.         } else if (typeof WisestampCompose != "undefined")
  356.         {
  357.             WisestampCompose.onUseSignature(null, aType, null);
  358.         } else if (typeof WisestampOverlay != "undefined")
  359.         {
  360.             this._saveSignatureToFile(aType, null);
  361.         }
  362.     },
  363.     
  364.     updateFramesFromChrome: function updateFramesFromChrome_name(aType)
  365.     {
  366.         WiseStampUtils.log("[appspecific.js::updateFramesFromChrome] >>>>> ");
  367.         
  368.         var PREFIX = "<!--WISESTAMP_SIG_START-->";
  369.         var SUFFIX = "<!--WISESTAMP_SIG_END-->";
  370.         function scanFrames(win)
  371.         {
  372.             for (var i = 0; i < win.frames.length; i++)
  373.             {
  374.                 var f = win.frames[i];
  375.                 if (f.document.designMode == "on")
  376.                 {
  377.                     var pattern = new RegExp(PREFIX.replace("!", "\!") + "(.|\n)*" + SUFFIX.replace("!", "\!"));
  378.                     var sig = PREFIX + WisestampSignatureFactory.createSignature(null, aType) + SUFFIX;
  379.                     var body = f.document.body;
  380.                     if (body.innerHTML.match(pattern))
  381.                     {
  382.                         body.innerHTML = body.innerHTML.replace(pattern, sig);
  383.                     } else
  384.                     {
  385.                         body.innerHTML = body.innerHTML + "<br><br>" + sig;
  386.                     }
  387.                 } else
  388.                 {
  389.                     scanFrames(f);
  390.                 }
  391.             }
  392.         }
  393.         var win = content ? content : window;
  394.         scanFrames(content);
  395.     },
  396.     
  397.     getFooter: function (strings)
  398.     {
  399.         //var footer = strings.GetStringFromName("wisestamp-footer-thunderbird");
  400.         var footer = strings.GetStringFromName("wisestamp-footer-firefox");
  401.         return footer;
  402.     },
  403.     
  404.     updateUI: function()
  405.     {
  406.         //document.getElementById("autoinsert_thread").setAttribute("disabled", "true");
  407.     }
  408. }